package cn.com.dhcc.app.pub.module.usermgr.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.base.BaseDao;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.pub.module.authmgr.service.AuthService;
import cn.com.dhcc.app.pub.module.authmgr.vo.RoleInfoInner;
import cn.com.dhcc.app.pub.module.usermgr.dao.RelUserRoleInfoDao;
import cn.com.dhcc.app.pub.module.usermgr.vo.RelUserRole;
/**
 * 用户角色管理
 * @author joe
 * @time  2016-5-17 下午4:56:42
 */
@Service
public class RelUserRoleInfoService extends BaseDao{

	@Autowired
	RelUserRoleInfoDao dao;
	@Autowired
	AuthService authService;
	public List<RoleInfoInner> getRoleListByUser(String userId) throws ServiceException {
		try {
			List<RoleInfoInner> roleInfoList = new ArrayList<RoleInfoInner>();
			List<RelUserRole> relSysUserRoleList = dao.getRelSysUserRoleListByUserId(userId);
			
			for (RelUserRole relSysUserRole : relSysUserRoleList) {
				RoleInfoInner roleInfo = authService.getRole(relSysUserRole.getRoleId());
				roleInfoList.add(roleInfo);
			}
			return roleInfoList;
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public List<RoleInfoInner> getOtherRoleList(String userName, String userType) throws ServiceException, NiceException {
		try {
			List<RoleInfoInner> otherRoleList = dao.getOtherRoleListByUserName(userName, userType);
			return otherRoleList;
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}

	public int deleteRelSysUserRole(RelUserRole vo) throws ServiceException {
		try {
			return dao.deleteRelSysUserRole(vo);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	public int insertOrUpdateUserRole(RelUserRole vo) throws ServiceException {
		try {
			return getDao().saveOrUpdateModuleByIdExecuteNull("添加或者修改用户角色", vo);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	public int insertBatchUserRole(List<RelUserRole> relUserRoleList) throws ServiceException {
		try {
			return getDao().batchInsertModule("批量添加用户角色", relUserRoleList);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
	public int countUserRole(WhereCondition parameter) throws ServiceException {
		try {
			return getDao().countModule("查询用户角色总数", RelUserRole.class, parameter);
		} catch (DaoException e) {
			throw new ServiceException(e.getMessage(), e);
		}
	}
}
